using Cysharp.Threading.Tasks;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public partial class ExitPanel : UIPanel
{
    private const string path = "Assets/Prefab/ExitPanel.prefab";

    public ExitPanel() : base(new UIType(path))
    {

    }

    public async override void OnEnter()
    {
        base.OnEnter();

        float moveDistance = ParentRectTransform.sizeDelta.x / 2 + UIObj.transform.parent.GetComponent<RectTransform>().sizeDelta.x / 2;

        ParentRectTransform.DOAnchorPosX(ParentRectTransform.anchoredPosition.x - moveDistance, 1);

        await UniTask.Delay(TimeSpan.FromSeconds(1));

        AddInputEventCallback();
    }

    public async override UniTask OnStop(bool useAnim)
    {
        await base.OnStop(useAnim);

        if (useAnim)
        {
            float moveDistance = ParentRectTransform.sizeDelta.x / 2 + UIObj.transform.parent.GetComponent<RectTransform>().sizeDelta.x / 2;

            ParentRectTransform.DOAnchorPosX(ParentRectTransform.anchoredPosition.x + moveDistance, 1);

            await UniTask.Delay(TimeSpan.FromSeconds(1));
        }

        RemoveInputEventCallback();
    }

    private void AddInputEventCallback()
    {
        m_ExitButton.onClick.AddListener(async() =>
        {
            await PanelManager.Instance.ClosePanel(this);
        });

        m_ComfirmButton.onClick.AddListener(() =>
        {
            Application.Quit();
        });
    }

    private void RemoveInputEventCallback()
    {
        m_ExitButton.onClick.RemoveAllListeners();
        m_ComfirmButton.onClick.RemoveAllListeners();
    }
}
